﻿/*
dateBox.js 2002-01-09

Author(s): Serge Ryabcuck, z555.com, Copyright 2002

z555.com grants you a royalty free license to use or modify this
software provided that this copyright notice appears on all copies.
This software is provided "AS IS," without a warranty of any kind.
*/


/* ToDo

 - Masks like dd/mm/yyyy, mm/dd/yyyy, etc.
 - Redraw of the object after change of object style properties.
 - Rewrite some object methods for conforming with initial idea  
   and remoe direct HTML objects calls for properties duplicates
   
*/

window.dbIE  = document.all ? true : false;                                // IE 4+
window.dbDOM = (document.getElementById && ! document.all) ? true : false; // NS6, Mozilla, other DOM2 compartible browsers


function dateBox(name, month, day, year,istartyear,iendyear) {
   var d = new Date();
   this.name            = name;
   this.day             = day;
   this.month           = month;
   this.year            = year;
   this.id;
   this.version         = "2.0.1 [Date Box; 20020109] ";
   this.type            = "dateBox";
   //this.startYear       = 1985;
   if(istartyear)
   this.startYear       = istartyear;
   else
   this.startYear       = 1985;
   
   if(iendyear)
   this.endYear       = iendyear;
   else
   this.endYear       = (d.getFullYear()-1);
   //this.endYear         = (d.getFullYear()-1);bannerLinkClick(3)
   this.height          = "auto";
   this.shortMonthWidth = 54;
   this.longMonthWidth  = 87;
   this.dayWidth        = 50;
   this.yearWidth       = 60;
   //this.fontFamily      = 'lucida grande,tahoma,verdana,arial,sans-serif';
   //this.fontSize        = '11px';
   this.dateBoxStyle    = 'short';

        
   this.shortMonth = [ 'Jan', 'Feb', 'Mar', 
                       'Apr', 'May', 'Jun', 
                       'Jul', 'Aug', 'Sep', 
                       'Oct', 'Nov', 'Dec'
                     ]; 

   this.longMonth  = [ 'January', 'February', 'March', 
                       'April', 'May', 'June', 
                       'July', 'August', 'September', 
                       'October', 'November', 'December'
                     ]; 
                                         
// Other Properties;                                     

   this.HTMLcontainer;
   this.objForm;
   this.objMonth;
   this.objDay;
   this.objYear;
        
// Methods
        
   this.getName            = getName;
        
   this.setDay             = setDay;
   this.getDay             = getDay;
        
   this.setMonth           = setMonth;
   this.getMonth           = getMonth;

   this.setYear            = setYear;
   this.getYear            = getYear;

   this.getID              = getID;
        
   this.setStartYear       = setStartYear;
   this.getStartYear       = getStartYear;
   this.setEndYear         = setEndYear;
   this.getEndYear         = getEndYear;
        
   this.getDateBoxStyle    = getDateBoxStyle;
        
   this.setHeight          = setHeight;
   this.getHeight          = getHeight;
        
   this.setShortMonth      = setShortMonth;
   this.getShortMonth      = getShortMonth;

   this.setLongMonth       = setLongMonth;
   this.getLongMonth       = getLongMonth;
        
   this.getMonthWidth      = getMonthWidth;
        
   this.setDayWidth        = setDayWidth;
   this.getDayWidth        = getDayWidth;
        
   this.setYearWidth       = setYearWidth;
   this.getYearWidth       = getYearWidth;

   this.getMonthName       = getMonthName;

   this.setFontFamily      = setFontFamily;
   this.getFontFamily      = getFontFamily;

   this.setFontSize        = setFontSize;
   this.getFontSize        = getFontSize;
        
   this.setObjPointers     = setObjPointers;
   this.makeDateHTML       = makeDateHTML;
   this.printHTML          = printHTML;
   this.monthDays          = monthDays;
   this.limitList          = limitList
        
   this.getObjForm         = getObjForm;
   this.getObjDay          = getObjDay;
   this.getObjMonth        = getObjMonth;
   this.getObjYear         = getObjYear;
        
   this.getObjSelectedDate = getObjSelectedDate;
   this.setRawDate         = setRawDate;
   this.setObjDate         = setObjDate;
                
//Events
   this.onSelectDate       = onSelectDate;

   var curDate = new Date();
   if (!month) { this.month = curDate.getMonth()+1 };
   if (!day)   { this.day   = curDate.getDate() };
   if (!year)  { 
       if (window.dbDOM) {              
           this.year  = curDate.getYear()+1900;
        } else {
           this.year  = curDate.getYear();
        }
   };
        
   if (!window.dateBoxes) window.dateBoxes = new Array(); 
   this.id=window.dateBoxes.length;
   window.dateBoxes[window.dateBoxes.length] = this;
        
}

/////////////////////////////////////////////////////////////
// dateBox.getName()
function getName() {
   return this.name;
}                

/////////////////////////////////////////////////////////////
// dateBox.setDay()
function setDay(day) {
   this.day=day;
}                

/////////////////////////////////////////////////////////////
// dateBox.getDay()
function getDay() {
   return this.day;
}                

/////////////////////////////////////////////////////////////
// dateBox.setMonth()
function setMonth(month) {
   this.month = month;
}                

/////////////////////////////////////////////////////////////
// dateBox.getMonth()
function getMonth() {
   return this.month;
}                

/////////////////////////////////////////////////////////////
// dateBox.setYear()
function setYear(year) {
   this.year=year;
}                

/////////////////////////////////////////////////////////////
// dateBox.getYear()
function getYear() {
   return this.year;
}                

/////////////////////////////////////////////////////////////
// dateBox.getID()
function getID() {
   return this.id;
}                

/////////////////////////////////////////////////////////////
// dateBox.setStartYear()
function setStartYear(year) {
   this.startYear = year;
}                

/////////////////////////////////////////////////////////////
// dateBox.getStartYear()
function getStartYear() {
   return this.startYear;
}                

/////////////////////////////////////////////////////////////
// dateBox.setEndYear()
function setEndYear(year) {
   this.endYear = year;
}                

/////////////////////////////////////////////////////////////
// dateBox.getEndYear()
function getEndYear() {
   return this.endYear;
}                

/////////////////////////////////////////////////////////////
// dateBox.getDateBoxStyle()
function getDateBoxStyle() {
   return this.dateBoxStyle;
}

/////////////////////////////////////////////////////////////
// dateBox.setShortMonth()
function setShortMonth(monthArray) {
   this.shortMonth=monthArray;
}

/////////////////////////////////////////////////////////////
// dateBox.getShortMonth()
function getShortMonth(monthIndex) {
   return this.shortMonth[monthIndex-1];
}

/////////////////////////////////////////////////////////////
// dateBox.setLongMonth()
function setLongMonth(monthArray) {
   this.longMonth=monthArray;
}

/////////////////////////////////////////////////////////////
// dateBox.getLongMonth()
function getLongMonth(monthIndex) {
   return this.longMonth[monthIndex-1];
}

/////////////////////////////////////////////////////////////
// dateBox.getMonthName()
function getMonthName(monthIndex) {
   if (this.getDateBoxStyle() == 'short') {
      return this.getShortMonth(monthIndex);
   } else {
      return this.getLongMonth(monthIndex);
   }
}

/////////////////////////////////////////////////////////////
// dateBox.setHeight()
function setHeight(height) {
   this.height = height;
}                

/////////////////////////////////////////////////////////////
// dateBox.getHeight()
function getHeight() {
   return this.height;
}

/////////////////////////////////////////////////////////////
// dateBox.setShortMonthWidth()
function setShortMonthWidth(width) {
   this.shortMonthWidth = width;
}                

/////////////////////////////////////////////////////////////
// dateBox.setLongMonthWidth()
function setLongMonthWidth(width) {
   this.longMonthWidth = width;
}                

/////////////////////////////////////////////////////////////
// dateBox.getMonthWidth()
function getMonthWidth() {
   if (this.getDateBoxStyle() == 'short') {
      return this.shortMonthWidth;
   } else {
      return this.longMonthWidth;
   }
}

/////////////////////////////////////////////////////////////
// dateBox.setDayWidth()
function setDayWidth(width) {
   this.dayWidth = width;
}                

/////////////////////////////////////////////////////////////
// dateBox.getDayWidth()
function getDayWidth() {
   return this.dayWidth;
}

/////////////////////////////////////////////////////////////
// dateBox.setYearWidth()
function setYearWidth(width) {
   this.yearWidth = width;
}                

/////////////////////////////////////////////////////////////
// dateBox.getYearWidth()
function getYearWidth() {
   return this.yearWidth;
}

/////////////////////////////////////////////////////////////
// dateBox.setFontFamily()
function setFontFamily(family) {
   this.fontFamily=family;
}

/////////////////////////////////////////////////////////////
// dateBox.getFontFamily()
function getFontFamily() {
   return this.fontFamily;
}

/////////////////////////////////////////////////////////////
// dateBox.setFontSize()
function setFontSize(size) {
   this.fontSize=size;
}

/////////////////////////////////////////////////////////////
// dateBox.getFontSize()
function getFontSize() {
   return this.fontSize;
}

/////////////////////////////////////////////////////////////
// dateBox.getObjForm()
function getObjForm() {
   return this.objForm;
}

/////////////////////////////////////////////////////////////
// dateBox.getObjDay()
function getObjDay() {
   return this.objDay;
}

/////////////////////////////////////////////////////////////
// dateBox.getObjMonth()
function getObjMonth() {
   return this.objMonth;
}

/////////////////////////////////////////////////////////////
// dateBox.getObjYear()
function getObjYear() {
   return this.objYear;
}

/////////////////////////////////////////////////////////////
// dateBox.makeDateHTML()
function makeDateHTML() {
   var dateStr = "";
                
   // Build Day
   dateStr += '<select name="' + this.getName() + 'Day' + 
              '" id="' + this.getName() + 'Day' + 
              '" style="font-family : ' + this.getFontFamily() + 
              '; height: ' + this.getHeight() + 'px; width: ' + 
              this.getDayWidth() + 'px; font-size: ' + this.getFontSize() + 
              ';"  onChange="window.dateBoxes[' + this.getID() + 
              '].onSelectDate()" class="text w70">';

   for (i=1; i<=31; i++) {
       if (this.getDay() == i) {
           dateStr += '<option selected value=' + i + '>'+i;
       } else {
         dateStr += '<option value=' + i + '>'+i;
       }
   }
   
   dateStr += "</select>&nbsp;";
    // Build Month
   dateStr += '<select name="' + this.getName() + 'Month' + 
              '" id="' + this.getName() + 'Month' + 
              '" style="font-family : ' + this.getFontFamily() + 
              '; height: ' + this.getHeight() + 'px; width:' +
              this.getMonthWidth() + 'px; font-size: ' + this.getFontSize() +
              ';" onChange="window.dateBoxes[' + this.getID() + '].onSelectDate()" class="text w70">';

   for (i=1; i<=12;i++) {
       if (this.getMonth() == i) {
          dateStr += '<option selected value=' + i + '>' + this.getMonthName(i);
       } else {
          dateStr += '<option value=' + i + '>' + this.getMonthName(i);
       }
   }
   dateStr += "</select>&nbsp;";
   
   // Build Year
   dateStr += '<select name="' + this.getName() + 'Year' + 
              '" id="' + this.getName() + 'Year' + 
              '" style="font-family : ' + this.getFontFamily() + '; height: ' + 
              this.getHeight() + 'px; width: ' + this.getYearWidth() + 
              'px; font-size: ' + this.getFontSize() + 
              ';" onChange="window.dateBoxes[' + this.getID() + '].onSelectDate()" class="text w70">';
                        
   for (i=this.getStartYear(); i<=this.getEndYear(); i++) {
        if (this.getYear() == i) {
           dateStr += '<option selected value=' + i + '>' + i;
        } else {
           dateStr += '<option value=' + i + '>' + i;
        }
   }
   dateStr += "</select>&nbsp;";
        
   this.HTMLcontainer=dateStr;     
}

/////////////////////////////////////////////////////////////
// dateBox.printHTML()
function printHTML() {
   document.write(this.HTMLcontainer);
   this.setObjPointers(document.forms[document.forms.length-1]);
   this.limitList(this.monthDays(this.getMonth(),this.getYear()));
}

/////////////////////////////////////////////////////////////
// dateBox.setObjPointers()
function setObjPointers(form) {
   this.objForm  = form;
   this.objDay   = eval("form."+this.getName()+"Day");
   this.objMonth = eval("form."+this.getName()+"Month");
   this.objYear  = eval("form."+this.getName()+"Year");
}

// How many days in the month?
/////////////////////////////////////////////////////////////
// dateBox.monthDays()
function monthDays(month,year) {
   var day = new Array(31,28,31,30,31,30,31,31,30,31,30,31);
   month--;
   if ((year % 4 == 0) && (month==1)) {
       if (year % 100 == 0) {
          if (year % 400 == 0) {
              return 29;
          } else {
              return 28;
          }
       } else {
          return 29;
       }   
  } else { 
     return day[month];
  }
}       

// Event processor
/////////////////////////////////////////////////////////////
// dateBox.onSelectDate()
function onSelectDate() {
   if (window.dbIE || window.dbDOM) {
      var objDay=this.getObjDay();
      var objYear=this.getObjYear();
      var objMonth=this.getObjMonth();

      yearVal=objYear.options[objYear.selectedIndex].text;
      monthVal=objMonth.options[objMonth.selectedIndex].value;
      this.limitList(this.monthDays(monthVal,yearVal));

      this.setDay(objDay.selectedIndex+1);
      this.setMonth(objMonth.selectedIndex+1);
      this.setYear(objYear.selectedIndex+this.startYear);
   }
} 

//Rebuilds dropdown list of day options according to the month
/////////////////////////////////////////////////////////////
// dateBox.limitList()
function limitList(length) {
   list=this.getObjDay();
   if (length<(list.selectedIndex+1)) {
      list.selectedIndex=length-1; 
   }
   if (window.dbIE || window.dbDOM) {
      if (list.options.length<length) {
         for (var i=list.options.length+1; i<=length; i++) { 
             var oOption = document.createElement('OPTION');
             if (window.dbIE) {
                list.options.add(oOption);
                oOption.innerText = i;
                oOption.Value = i;
             } else if (window.dbDOM) {      
                oOption.text = ' '+i;
                oOption.Value = i;
                list.add(oOption,null);
             }
          }
      } else if (list.options.length>length) {
         for (var i=list.options.length; i>=length; i--) { 
             list.remove(i);
         }               
      }
  }
}

// Convert form fields to Date object
/////////////////////////////////////////////////////////////
// dateBox.getObjSelectedDate()
function getObjSelectedDate() {
   if (window.dbIE || window.dbDOM) {
      var objDay=this.getObjDay();
      var objYear=this.getObjYear();
      var objMonth=this.getObjMonth();
                 
      var day=objDay.options[objDay.selectedIndex].text;
      var month=objMonth.options[objMonth.selectedIndex].value-1;
      var year=objYear.options[objYear.selectedIndex].text;
                 
      var dateObj = new Date(year, month, day);
      return dateObj;
   }
}

// Set specified Date
/////////////////////////////////////////////////////////////
// dateBox.setRawDate()
function setRawDate(month,day,year) {
   if (window.dbIE || window.dbDOM) {
      var objDay=this.getObjDay();
      var objYear=this.getObjYear();
      var objMonth=this.getObjMonth();

      this.limitList(this.monthDays(month,year));                    
      objDay.selectedIndex=day-1;
      objMonth.selectedIndex=month-1;
      objYear.selectedIndex=year-this.startYear;
   }
}

/////////////////////////////////////////////////////////////
// dateBox.setObjDate()
function setObjDate(date){
   if (window.dbIE || window.dbDOM) {
       var month = date.getMonth()+1;
       var day   = date.getDate();
       if (window.dbDOM) {             
          var year  = date.getYear()+1900;
       } else {
          var year  = date.getYear();
       }
       this.setRawDate(month,day,year);
   }
}